package com.neu.service.impl;

import com.neu.service.CartService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;



@Service
public class CartServiceImpl implements CartService {

    //k                            v[hash]
    //cart:customerId:shopId      k            v
                               //productId      count
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public String plus(int customerId, int shopId, int foodId) {


        String customerKey = "cart:"+customerId+":"+shopId;
        String foodKey=""+foodId;

        if(redisTemplate.opsForHash().hasKey(customerKey,foodKey)){
            redisTemplate.opsForHash().increment(customerKey,foodKey,new Integer(1));
            return "成功";
        }else {
            redisTemplate.opsForHash().put(customerKey,foodKey,new Integer(1));
        }
        return "成功";
    }

    @Override
    public String minus(int customerId, int shopId, int foodId) {

        String customerKey = "cart:"+customerId+":"+shopId;
        String foodKey=""+foodId;
        //
        if(redisTemplate.opsForHash().hasKey(customerKey,foodKey)) {
            redisTemplate.opsForHash().increment(customerKey, foodKey, new Integer(-1));
            return "成功";
        }
        return "成功";
    }
}
